<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="工单编号" prop="workNo">
        <el-input
          v-model="queryParams.workNo"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['big/dcs/req:order:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['big/dcs/req:order:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['big/dcs/req:order:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['big/dcs/req:order:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="downloadTemplate"
          >下载导入模板</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <MyUpload
          buttonName="导入模板"
          url="/"
          @success="handleUploadSuccess"
          @warning="handleUploadWarning"
        />
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
      show-overflow-tooltip
      :tooltip-formatter="tableRowFormatter"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="工单编号" align="center" prop="workNo" />
      <el-table-column label="料号" align="center" prop="partId" />
      <el-table-column label="料号名称" align="center" prop="partName" />
      <el-table-column label="工单数量" align="center" prop="workQty" />
      <el-table-column
        label="允收数量"
        align="center"
        prop="receivedQty"
      />
      <el-table-column label="报废数量" align="center" prop="scrapQty" />
      <el-table-column label="隶属厂区" align="center" prop="factory" />
      <el-table-column label="成本中心" align="center" prop="costCenter" />
      <el-table-column
        label="成本中心描述"
        align="center"
        prop="costCenterDesc"
      />
      <el-table-column
        label="开工日期"
        align="center"
        prop="woDate"
      />
      <el-table-column
        label="工单结案状态"
        align="center"
        prop="woStatus"
      />
      <el-table-column
        label="备料状态"
        align="center"
        prop="callStatus"
      />
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['big/dcs/req:order:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['big/dcs/req:order:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求单单头对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="工单编号" prop="workNo">
          <el-input v-model="form.workNo" placeholder="请输入工单编号" />
        </el-form-item>
        <el-form-item label="料号" prop="partId">
          <el-input v-model="form.partId" placeholder="请输入料号" />
        </el-form-item>
        <el-form-item label="料号名称" prop="partName">
          <el-input v-model="form.partName" placeholder="请输入料号名称" />
        </el-form-item>
        <el-form-item label="工单数量" prop="workQty">
          <el-input v-model="form.workQty" placeholder="请输入工单数量" />
        </el-form-item>
        <el-form-item label="允收数量" prop="receivedQty">
          <el-input
            v-model="form.receivedQty"
            placeholder="请输入允收数量"
          />
        </el-form-item>
        <el-form-item label="报废数量" prop="scrapQty">
          <el-input v-model="form.scrapQty" placeholder="请输入报废数量" />
        </el-form-item>
        <el-form-item label="隶属厂区" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入隶属厂区" />
        </el-form-item>
        <el-form-item label="成本中心" prop="costCenter">
          <el-input v-model="form.costCenter" placeholder="请输入成本中心" />
        </el-form-item>
        <el-form-item label="成本中心描述" prop="costCenterDesc">
          <el-input
            v-model="form.costCenterDesc"
            placeholder="请输入成本中心描述"
          />
        </el-form-item>
        <el-form-item label="开工日期" prop="woDate">
          <el-input
            v-model="form.woDate"
            placeholder="请输入开工日期"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/work/scheduleOrder";

const { proxy } = getCurrentInstance();

const orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    workNo: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

const tableRowFormatter = (data) => {
  return `${data.cellValue}`
}

/** 查询需求单单头列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then((res) => {
    if (res.code === 200) {
      orderList.value = res.rows;
      total.value = res.total;
      loading.value = false;
    }
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    workNo: null,
    partId: null,
    partName: null,
    workQty: null,
    receivedQty: null,
    scrapQty: null,
    factory: null,
    costCenter: null,
    costCenterDesc: null,
    woDate: null,
    woStatus: null,
    callStatus: null,
    createBy: null,
    updateBy: null,
    createTime: null,
    updateTime: null,
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加需求单单头";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getOrder(_id).then((res) => {
    form.value = res.data;
    open.value = true;
    title.value = "修改需求单单头";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateOrder(form.value).then((res) => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          } else {
            proxy.$modal.msgError(res.msg);
          }
        });
      } else {
        addOrder(form.value).then((res) => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          } else {
            proxy.$modal.msgError(res.msg);
          }
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除需求单单头编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delOrder(_ids);
    })
    .then((res) => {
      if (res.code === 200) {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      } else {
        proxy.$modal.msgError(res.msg);
      }
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "big/dcs/req/order/export",
    {
      ...queryParams.value,
    },
    `order_${new Date().getTime()}.xlsx`
  );
}

getList();

// 下载模板
const downloadTemplate = async () => {
  proxy.download("/", {}, `order模板_${new Date().getTime()}.xlsx`);
};
// 上传成功后的回调
const handleUploadSuccess = (res) => {
  getList();
};
// 部分成功回调
const handleUploadWarning = (res) => {
  getList();
};
</script>
